using System;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Animation.Easings;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.OpenGL;
using Avalonia.Styling;
using ReactiveUI;
using SocketTool.Model.BindingModels;

namespace SocketTool.Views.Protocols
{
    public partial class ProtocolManageView : UserControl
    {
        public ProtocolManageView()
        {
            InitializeComponent();
            GlobalContext.Current.CurrentProtocolManageView = this;
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        /// <summary>
        /// 分组重命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProtocolGroupRename_Click(object? sender, RoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            if (menuItem == null || menuItem.DataContext == null)
                return;
            var groupModel = menuItem.DataContext as ProtocolGroupBindingModel;
            groupModel.IsEditing = true;
        }

        /// <summary>
        /// 删除分组信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProtocolGroupDelete_Click(object? sender, RoutedEventArgs e)
        { 
            var menuItem = sender as MenuItem;
            if (menuItem == null || menuItem.DataContext == null)
                return;
            
            this.DtContext.ProtocolGroupRemove(menuItem.DataContext as ProtocolGroupBindingModel);
        }
        
        /// <summary>
        /// 分组重命名框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void InputElement_OnKeyDown(object? sender, KeyEventArgs e)
        {
            var tbx = sender as TextBox;
            if(tbx.DataContext == null)
                return;
            var groupModel = tbx.DataContext as ProtocolGroupBindingModel;
            if (e.Key == Key.Escape)
            {
                groupModel.IsEditing = false;
            }
            if (e.Key == Key.Enter)
            {
                string text = tbx.Text;
                if (string.IsNullOrEmpty(text) || text.Trim().Length > 15)
                {
                    DialogWindows.MessageDialogWindow.ShowTipsDialog("分组名称格式错误！");
                    return;
                }

                this.DtContext.ProtocolGroupReName(groupModel);
            }
        }
        
        /// <summary>
        /// 失去焦点时保存命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InputElement_OnLostFocus(object? sender, RoutedEventArgs e)
        {
            // var tbx = sender as TextBox;
            // if(tbx.DataContext == null)
            //     return;
            // var groupModel = tbx.DataContext as ProtocolGroupBindingModel;
            // string text = tbx.Text;
            // if (string.IsNullOrEmpty(text) || text.Trim().Length > 15)
            // {
            //     DialogWindows.MessageDialogWindow.ShowTipsDialog("分组名称格式错误！");
            //     e.Handled = true;
            //     return;
            // }
            // groupModel.GroupName = groupModel.EditName;
            // groupModel.IsEditing = false;
            // this.DtContext.ProtocolGroupReName(groupModel);
        }
        
        /// <summary>
        /// 点击空白地区隐藏协议编辑框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
        {
            if(e.Source.GetType() != typeof(Border) || !(e.Source as Border).Classes.Contains("Drawer"))
                return;
            this.DtContext.IsShowEdit = false;
        }

        /// <summary>
        /// 获取协议编辑控件的ViewModel
        /// </summary>
        /// <returns></returns>
        public ViewModels.ControlViewModel.ReqMessageParamViewModel GetReqParamVMContext()
        {
            return SocketTool.Controls.ReqMessageParamControl.GetVMContext(this, "ParamControl");
        }
        
        
    }

    public partial class ProtocolManageView : UserControl
    {
        private ViewModels.Protocols.ProtocolManageViewModel mDtContext;

        public ViewModels.Protocols.ProtocolManageViewModel DtContext
        {
            get
            {
                if(this.mDtContext == null)
                    this.mDtContext = this.DataContext as ViewModels.Protocols.ProtocolManageViewModel;
                return this.mDtContext;
            }
        }
        
    }
}